<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item :label="$t('equipment.programName')" prop="programName">
        <el-input
            v-model="queryParams.programName"
            :placeholder="$t('equipment.programNameInput')"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('terminal.organization')" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="deptOptions" :default-expand-level="2"
                    :normalizer="deptNormalizer" style="width: 198px;" placeholder="请选择组织机构"
        />
      </el-form-item>
      <el-form-item :label="$t('terminal.issuingTime')">
        <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 240px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            :start-placeholder="$t('elButton.startTime')"
            :end-placeholder="$t('elButton.endTime')"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{
            $t('elButton.search')
          }}
        </el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('elButton.reset') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            v-hasPerm="['downloadStat_export']"
            type="warning"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
        >{{ $t('elButton.export') }}
        </el-button>
      </el-col>
      <div class="top-right-btn">
        <el-tooltip class="item" effect="dark" :content="$t('elButton.refresh')" placement="top">
          <el-button size="mini" circle icon="el-icon-refresh" @click="handleQuery"/>
        </el-tooltip>
        <el-tooltip class="item" effect="dark"
                    :content="showSearch ? $t('elButton.hideSearch') : $t('elButton.showSearch')" placement="top"
        >
          <el-button size="mini" circle icon="el-icon-search" @click="showSearch=!showSearch"/>
        </el-tooltip>
      </div>
    </el-row>

    <el-table v-loading="loading" :data="downloadList" border @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column :label="$t('program.programNumber')" align="center" prop="programId"/>
      <el-table-column :label="$t('equipment.programName')" align="center" prop="programName"
                       :show-overflow-tooltip="true"
      />
      <!-- <el-table-column :label="$t('equipment.programTimeWiths')" align="center" prop="duration"/>-->
      <el-table-column :label="$t('terminal.allIssuing')" align="center" prop="total"/>
      <el-table-column :label="$t('equipment.control')" align="center" class-name="small-padding fixed-width"
                       width="120" fixed="right"
      >
        <template slot-scope="scope">
          <el-button
              v-hasPerm="['downloadStat_view']"
              size="mini"
              type="text"
              icon="el-icon-info"
              @click="handleDetail(scope.row)"
          >{{ $t('elButton.detailed') }}
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.current"
        :limit.sync="queryParams.size"
        @pagination="getList"
    />

    <!-- 明细对话框 -->
    <el-dialog :title="title" :visible.sync="open" :modal-append-to-body="true" :append-to-body="true"
               :fullscreen="true"
    >
      <el-table
          :data="detailList"
          style="width: 100%"
          :border="true"
          :stripe="true"
      >
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column :label="$t('program.programNumber')" align="center" prop="programId"/>
        <el-table-column :label="$t('equipment.programName')" align="center" prop="programName"/>
        <!--<el-table-column :label="$t('equipment.programTimeWiths')" align="center" prop="duration"/>-->
        <el-table-column :label="$t('program.distributionTimes')" align="center" prop="dloadTimes"/>
        <el-table-column :label="$t('equipment.identification')" align="center" prop="deviceCode"
                         :show-overflow-tooltip="true"
        />
        <el-table-column :label="$t('equipment.name')" align="center" prop="deviceName" :show-overflow-tooltip="true"/>
        <el-table-column :label="$t('equipment.channelName')" align="center" prop="schedulingName"
                         :show-overflow-tooltip="true"
        />
      </el-table>
      <pagination
          v-show="detailTotal>0"
          :total="detailTotal"
          :page.sync="detailParams.current"
          :limit.sync="detailParams.size"
          @pagination="getDetailList"
      />
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">{{ $t('closeText') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDownload,
  statList,
  exportDownload
} from '@/api/ips/download'
import { deptTree } from '@/api/system/dept'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'

export default {
  name: 'DownloadStat',
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 节目下发表格数据
      downloadList: [],
      detailTotal: 0,
      detailList: [],
      // 机构名称树选项
      deptOptions: undefined,
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        programName: undefined,
        createTime: undefined
      },
      detailParams: {
        current: 1,
        size: 10,
        deviceCode: undefined,
        programId: undefined
      },
      // 显示搜索条件
      showSearch: true,
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    }
  },
  created() {
    this.getList()
    this.getDeptTree()
  },
  methods: {
    /** 查询节目下发列表 */
    getList() {
      this.loading = true
      statList(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.downloadList = response.data
        this.total = response.total
        this.loading = false
      })
    },
    getDetailList() {
      this.loading = true
      listDownload(this.addDateRange(this.detailParams, this.dateRange)).then(response => {
        this.detailList = response.data
        this.detailTotal = response.total
        this.loading = false
      })
    },
    /** 转换机构数据结构 */
    deptNormalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children
      }
      return {
        id: node.deptId,
        label: node.name,
        children: node.children
      }
    },
    /** 查询机构名称下拉树结构 */
    getDeptTree() {
      deptTree().then(response => {
        if (response.data && response.data.length > 0) {
          this.deptOptions = this.handleTree(response.data, 'deptId', 'parentId', 'children', response.data[0].parentId)
        } else {
          this.deptOption = []
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        programName: undefined,
        createTime: undefined
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleDetail(row) {
      this.open = true
      this.title = '节目：' + row.programName + ' 下发明细'
      this.detailParams.programId = row.programId
      this.getDetailList()
    },
    /** 导出按钮操作 */
    handleExport() {
      if (this.ids.length === 0) {
        this.msgWarning(this.$t('equipment.equipmentInput'))
        return
      }
      const queryParams = this.addDateRange(this.queryParams, this.dateRange)
      this.$confirm('是否确认导出选中的节目下发数据项?', this.$t('warning'), {
        confirmButtonText: this.$t('submitText'),
        cancelButtonText: this.$t('cancelText'),
        type: 'warning'
      }).then(() => {
        return exportDownload(queryParams, this.ids)
      }).then(response => {
        this.download(response.data)
      }).catch(function() {
      })
    }
  }
}
</script>
